﻿using System.IO;
using System;
using System.Collections.Generic;
using System.Text;

namespace xReplace
{
    class InputObject
    {
        public InputObject(Project p, string overrideSourcePath)
        {
                this.path = p.GetKey("input", "path");
                if (overrideSourcePath != "") this.path = overrideSourcePath;
                if (!this.path.EndsWith("\\")) this.path += "\\";
                this.pattern = p.GetKey("input", "pattern");
                this.recursive = p.GetKeyAsBoolean("input", "recursive");
                this.exclude = p.GetKey("input", "exclude");
                this.include = p.GetKey("input", "include");
                this.includeDepth = p.GetKey("input", "includeDepth");
                this.excludeDepth = p.GetKey("input", "excludeDepth");
                this.doNotReadContent = p.GetKeyAsBoolean("input", "ignoreContent");
        }
        public string path;
        public string pattern;
        public bool recursive;
        public string exclude;
        public string include;
        public string includeDepth;
        public string excludeDepth;
        public bool doNotReadContent;

        public string[] GetFiles()
        {
            return (this.recursive) ? Directory.GetFiles(this.path, this.pattern, SearchOption.AllDirectories) : Directory.GetFiles(this.path, this.pattern, SearchOption.TopDirectoryOnly);
        }

        public bool CanInclude(string filename)
        {
            bool result = true;

            string[] includeKeys = null;
            string[] excludeKeys = null;
            string[] includeDepthKeys = null;
            string[] excludeDepthKeys = null;

            if (this.include != "") includeKeys = this.include.Split(',');
            if (this.exclude != "") excludeKeys = this.exclude.Split(',');
            if (this.includeDepth != "") includeDepthKeys = this.includeDepth.Split(',');
            if (this.excludeDepth != "") excludeDepthKeys = this.excludeDepth.Split(',');


            int baseDepth = this.path.Split('\\').Length;
            int currentDepth = filename.Split('\\').Length;
            int depth = currentDepth - baseDepth;

            if (includeDepthKeys != null)
            {
                result = false;
                for (int i = 0; i < includeDepthKeys.Length; i++)
                {
                    try
                    {
                        if (int.Parse(includeDepthKeys[i].Trim()) == depth)
                        {
                            result = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Error processing depth command: " + includeDepthKeys[i]);
                    }
                }
                if (!result) return false;
            }

            if (excludeDepthKeys != null)
            {
                result = true;
                for (int i = 0; i < excludeDepthKeys.Length; i++)
                {
                    try
                    {
                        if (int.Parse(excludeDepthKeys[i].Trim()) == depth)
                        {
                            result = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Error processing depth command: " + excludeDepthKeys[i]);
                    }
                }
                if (!result) return false;
            }

            result = true;
            if (includeKeys != null)
            {
                result = false;
                for (int i = 0; i < includeKeys.Length; i++)
                {
                    if (filename.IndexOf(includeKeys[i].Trim()) > -1)
                        result = true;
                }
                if (result) return true;
            }

            if (excludeKeys != null)
            {
                result = true;
                for (int i = 0; i < excludeKeys.Length; i++)
                {
                    if (filename.IndexOf(excludeKeys[i].Trim()) > -1)
                        return false;
                }
            }

            return result;
        }
    }
}
